<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-card class="box-card">
      <el-button type="primary" style="margin-bottom:20px" @click="addList">添加</el-button>

      <el-table
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        v-loading="loading"
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="name" label="名称"></el-table-column>
        <el-table-column align="center" prop="money" label="金额"></el-table-column>
        <el-table-column align="center" label="是否上架">
          <template slot-scope="scop">
            <div v-if="scop.row.is_show==1">是</div>
            <div v-if="scop.row.is_show==0">否</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="添加时间"></el-table-column>
        <el-table-column align="center" prop="update_time" label="更新时间"></el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleEdite(scope.row)"
              type="text"
              size="small"
            >编辑</el-button>
            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDel(scope.row)"
            >
              <el-button
              style="margin-left:10px;color:red" slot="reference" type="text" size="small">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" style="margin-left:50px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="是否上架" prop="is_show">
          <el-select v-model="form.is_show" placeholder="请选择是否上架">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="subCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { saveList, delSave, addSave, editeSave } from "@/api/system";
import { parseTime } from "@/utils/index";

export default {
  name: "Save",
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      // 表数据
      tableData: [],
      // 编辑抽屉
      drawer: false,
      direction: "rtl",
      title: "编辑",
      //   表单
      form: {
        name: "",
        money: "",
        is_show: ""
      },
      rules: {
        name: [{ required: true, message: "请选择输入名称", trigger: "blur" }],
        money: [
          { required: true, message: "请选输入金额", trigger: "blur" }
        ],
        is_show: [{ required: true, message: "请选择是否上架", trigger: "blur" }]
      },
      loading: false
    };
  },
  mounted() {
    this.getSaveList();
  },
  methods: {
    // 获取储值列表
    async getSaveList() {
      this.loading = true;
      const res = await saveList({
        limit: this.pageSize,
        page: this.currentPage
      });
      this.loading = false;
      console.log(res);
      this.tableData = res.data;
      this.tableData.forEach(item => {
        if (item.add_time == 0) {
          item.add_time = "-";
        } else {
          item.add_time = parseTime(item.add_time, "{y}-{m}-{d} {h}:{i}:{s}");
        }
        if (item.update_time == 0) {
          item.update_time = "-";
        } else {
          item.update_time = parseTime(
            item.update_time,
            "{y}-{m}-{d} {h}:{i}:{s}"
          );
        }
      });
      this.total = res.count;
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current;
      this.getSaveList();
    },
    // 添加储值
    addList() {
      this.drawer = true;
      this.title = "添加";
    },
    // 表格操作
    async handleDel(row) {
      try {
        const res = await delSave({ id: row.id });
        console.log(res);
        this.getSaveList();
        this.$message({
          message: "删除成功",
          type: "success"
        });
      } catch (error) {
        this.$message.error("删除失败");
        console.log(error);
      }
    },
    handleEdite(row) {
      this.drawer = true;
      this.title = "编辑";
      console.log(row);
      this.form.name = row.name;
      this.form.money = row.money;
      this.form.is_show = row.is_show;
      this.form.id = row.id;
    },
    //抽屉
    handleClose(done) {
      this.resetForm("form");
      const form = {
        name: "",
        money: "",
        is_show: "",
        id: ""
      };
      this.form = form;
    },
    subCancel() {
      this.resetForm("form");
      const form = {
        name: "",
        money: "",
        is_show: "",
        id: ""
      };
      this.form = form;
      this.drawer = false;
    },
    onSubmit() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          if (this.title === "添加") {
            console.log(this.form);
            const res = await addSave({ ...this.form });
            console.log(res);
            if (res.st === 1) {
              this.resetForm("form");
              const form = {
                name: "",
                money: "",
                is_show: ""
              };
              this.form = form;
              this.getSaveList();
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.drawer = false;
            } else {
              console.log(res);
              this.$message.error("添加失败");
            }
          } else if (this.title === "编辑") {
            const res = await editeSave({ ...this.form });
            console.log(res);
            if (res.st === 1) {
              this.resetForm("form");
              const form = {
                name: "",
                money: "",
                is_show: ""
              };
              this.form = form;
              this.getSaveList();
              this.$message({
                message: "编辑成功",
                type: "success"
              });
              this.drawer = false;
            } else {
              console.log(res);
              this.$message.error("编辑失败");
            }
          }
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.drawer = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>